package com.bdqn.springbootshior.config;

import com.bdqn.springbootshior.entity.User;
import com.bdqn.springbootshior.service.IUserService;
import jakarta.annotation.Resource;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
public class MyShiroRealm extends AuthorizingRealm {//安全数据源
    @Resource
    private IUserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("调用MyShiroRealm的doGetAuthenticationInfo获取身份信息");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String usrName=token.getUsername();
        User user=userService.getUserByUsrName(usrName);
        if(user==null){
            throw new AuthenticationException();
        }
        if(user.getUsrFlag()==null || user.getUsrFlag().intValue()==0){
            throw new LockedAccountException();
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                user,
                user.getUsrPassword(),
                getName());
        return info;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("调用MyShiroRealm的doGetAuthorizationInfo获取权限信息");
        User user = (User) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        return info;
    }
}
